<template>
  <div class="tags-container">
    <el-card shadow="never">
      <!-- 面包屑 -->
      <template #header v-if="subject.id && subject.name">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{subject.name}}</el-breadcrumb-item>
          <!-- <el-breadcrumb-item>具体学科</el-breadcrumb-item> -->
          <el-breadcrumb-item>目录管理</el-breadcrumb-item>
        </el-breadcrumb>
      </template>
      <br />

      <!-- input框 -->
      <el-row>
        <el-col :span="18">
          <el-form label-width="80px" inline size="small">
            <el-form-item label="目录名称">
              <el-input v-model="requestParams.tagName"> </el-input>
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="requestParams.state">
                <el-option :value="1" label="启用"></el-option>
                <el-option :value="0" label="禁用"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button @click="clear">清除</el-button>
              <el-button type="primary" @click="filter">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6" style="text-align: right">
          <!-- TODO  返回学科 -->
          <el-button v-if="subject.id && subject.name" type="text" icon="el-icon-back"
          @click="$router.back()">返回学科</el-button>
          <el-button type="success" size="small" @click="openDialog()">
            <i class="el-icon-edit"> 新增目录</i>
          </el-button>
        </el-col>
      </el-row>

      <!-- 提示消息 -->
      <el-alert
        class="alert"
        :title="`数据一共${total}条`"
        type="info"
        show-icon
      >
      </el-alert>

      <el-table style="width: 100%" :data="tags">
        <el-table-column
          label="序号"
          type="index"
          width="80px"
        ></el-table-column>
        <el-table-column label="所属学科" prop="subjectName"></el-table-column>
        <el-table-column label="标签名称" prop="tagName"></el-table-column>
        <el-table-column label="创建者" prop="username"></el-table-column>
        <el-table-column label="创建日期">
          <template #default="{ row }">
            {{ row.addDate | parseTimeByString }}
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template #default="{ row }">
            {{ row.state === 1 ? "已启用" : "已禁用" }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <!-- <template #default="{row}"> -->
          <template #default="{ row }">
            <el-button type="text" @click="toggleState(row)">
              {{ row.state === 1 ? "禁用" : "启用" }}</el-button
            >
            <el-button
              type="text"
              @click="openDialog(row)"
              :disabled="row.state === 1"
              >修改</el-button
            >
            <el-button type="text" @click="del(row)" :disabled="row.state === 1"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        style="text-align: right"
        background
        hide-on-single-page
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="requestParams.page"
        :page-sizes="[2,5, 10, 15, 20]"
        :page-size="requestParams.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :size-change="handleSizeChange"
        :current-change="handleCurrentChange"
      >
      </el-pagination>
    </el-card>

    <!-- 添加|修改 -->
    <TagsAdd
      ref="TagsAdd"
      :isShow.sync="isShow"
      :data="currTag"
      @updateList="updateList"
    ></TagsAdd>
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/tags'
import TagsAdd from '../components/tags-add.vue'
export default {
  name: 'Tags',
  components: { TagsAdd },
  data () {
    return {
      requestParams: {
        tagName: null,
        state: null,
        page: 1,
        pagesize: 10
      },
      tags: [],
      total: 0,
      isShow: false,
      currTag: {}
    }
  },
  computed: {
    subject () {
      return this.$route.query || {}
    }
  },
  created () {
    this.getList()
  },
  watch: {
    '$route.query': function () {
      this.getList()
    }
  },
  methods: {
    // 渲染列表(查询列表)
    async getList () {
      this.requestParams.subjectID = this.subject.id || null
      const { data } = await list(this.requestParams)
      console.log(data)
      console.log(data.items)
      this.tags = data.items
      console.log(this.tags)
      this.total = data.counts
    },

    // pageSize改变时会触发   参数为每页条数
    handleSizeChange (value) {
      console.log(value)
      this.requestParams.pagesize = value
      this.requestParams.page = 1 // 回到第一页
      this.getList()
    },

    // currentPage 改变时会触发  参数为当前页
    handleCurrentChange (value) {
      this.requestParams.page = value // 跳到当前页
      console.log(value)
      this.getList()
    },

    // 点击 启用 修改状态
    async toggleState (row) {
      await changeState({
        id: row.id,
        state: row.state === 1 ? 0 : 1
      })
      this.$message.success('操作成功')
      row.state = row.state === 1 ? 0 : 1
      // this.requestParams.state = row.state === 1 ? 0 : 1
      console.log(row.state)
    },

    // 清除input框内容
    clear () {
      console.log('清除搜索内容')
      this.requestParams = {
        tarName: null,
        state: null,
        page: 1,
        pagesize: 10
      }
      this.getList()
    },

    // 点击搜索
    filter () {
      console.log('搜索内容')

      // 获取表单的内容，传参，查找(接口)，渲染
      this.requestParams.page = 1 // 跳转到第一页
      this.getList()
    },

    // 删除该行内容
    del (row) {
      this.$confirm('确定删除嘛？？？', '温馨提示', { type: 'warning' })
        .then(async () => {
          this.$message.success('删除成功')
          const res = await remove({ id: row.id })
          console.log(res)
          if (this.tags.length === 1 && this.requestParams.page > 1) {
            this.requestParams.page--
          }
          this.getList()
        })
        .catch(() => {
          console.log('取消删除')
        })
    },
    // 打开对话框
    openDialog (tag = {}) {
      this.isShow = true
      this.currTag = tag // 当前行的数据
      // this.$nextTick(() => {
      //   this.$refs.TagsAdd.open()
      // })
    },

    // 新增|修改 后更新列表
    updateList () {
      if (!this.currTag.id) {
        this.requestParams.page = 1
      }
      this.getList()
    }
  }
}
</script>

<style scoped lang='scss'>
.tags-container {
  padding: 10px;
}
// .el-row {
//   margin-bottom: 20px;
// }
.alert {
  margin-bottom: 10px;
}
.pagination {
  margin-top: 10px;
}
</style>
